<template>
  <div class="register-container">
    <div class="register-background"></div>
    <el-card class="register-card">
      <template #header>
        <h2 class="register-title">注册账号</h2>
      </template>
      <el-form :model="registerForm" label-width="100px" :rules="rules" ref="formRef">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="registerForm.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="loginPassword">
          <el-input v-model="registerForm.loginPassword" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="confirmPassword" type="password" placeholder="请再次输入密码"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phoneNumber">
          <el-input v-model="registerForm.phoneNumber" placeholder="请输入联系电话"></el-input>
        </el-form-item>

        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="registerForm.companyName" placeholder="请输入公司名称"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleRegister" style="width: 100%">注册</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="goToLogin" style="width: 100%">返回登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { register } from '../../api/userApi'

const router = useRouter()
const formRef = ref(null)
const confirmPassword = ref('')

const registerForm = ref({
  companyName: "",
  createBy: 1,
  createTime: new Date().toISOString(),
  loginPassword: "",
  name: "",
  phoneNumber: "",
  status: "pending",
  updateBy: 1,
  updateTime: new Date().toISOString(),
  userId: 0
})

const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    if (confirmPassword.value !== '') {
      formRef.value.validateField('confirmPassword')
    }
    callback()
  }
}

const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (confirmPassword.value !== registerForm.value.loginPassword) {
    console.log(confirmPassword.value,registerForm.value.loginPassword);
    callback(new Error('两次输入密码不一致!'))
  } else {
    callback()
  }
}

const rules = {
  // phoneNumber: [
  //   { required: true, message: '请输入联系电话', trigger: 'blur' },
  //   { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  // ],
  loginPassword: [{ validator: validatePass, trigger: 'blur' }],
  confirmPassword: [{ validator: validatePass2, trigger: 'blur' }],
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  companyName: [{ required: true, message: '请输入公司名称', trigger: 'blur' }]
}

const handleRegister = async () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        const currentTime = new Date().toISOString()
        
        const response = await register(registerForm.value)
        if (response.flag) {
          ElMessage.success('注册申请提交成功，请等待管理员审核')
          router.push('/login')
        } else {
          ElMessage.error(response.reason || '注册失败，请重试')
        }
      } catch (error) {
        console.error('注册失败:', error)
        ElMessage.error('注册失败，请重试')
      }
    }
  })
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  padding: 20px 0;
}

.register-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  z-index: 0;
}

.register-background::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320"><path fill="rgba(255,255,255,0.1)" fill-opacity="1" d="M0,96L48,112C96,128,192,160,288,160C384,160,480,128,576,112C672,96,768,96,864,112C960,128,1056,160,1152,160C1248,160,1344,128,1392,112L1440,96L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z"></path></svg>') no-repeat bottom;
  background-size: cover;
  opacity: 0.6;
}

.register-card {
  width: 500px;
  position: relative;
  z-index: 1;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border: none;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.register-title {
  color: #1890ff;
  margin: 0;
  font-size: 24px;
  font-weight: 600;
}

.register-card :deep(.el-card__header) {
  text-align: center;
  background: rgba(255, 255, 255, 0.8);
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}

.register-card :deep(.el-input__wrapper) {
  background: rgba(255, 255, 255, 0.8);
}

.register-card :deep(.el-button--primary) {
  background: #1890ff;
  border: none;
  height: 40px;
  font-size: 16px;
  transition: all 0.3s ease;
}

.register-card :deep(.el-button--primary:hover) {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.4);
}

.register-card :deep(.el-button--default) {
  border: 1px solid #1890ff;
  color: #1890ff;
  height: 40px;
  font-size: 16px;
  transition: all 0.3s ease;
}

.register-card :deep(.el-button--default:hover) {
  background: rgba(24, 144, 255, 0.1);
}

.register-card :deep(.el-form-item__label) {
  color: #333;
  font-weight: 500;
}

.ml-2 {
  margin-left: 8px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 